<?php

/**
 * This is the model class for table "host".
 *
 * The followings are the available columns in table 'host':
 * @property integer $id_host
 * @property string $name
 * @property string $description
 * @property integer $uncategorized
 */
class Host extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Host the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'host';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name', 'unique', 'className' => __CLASS__, 'attributeName' => 'name', 'message' => 'name already exists'),
			array('uncategorized', 'numerical', 'integerOnly'=>true),
			array('name, description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_host, name, description, uncategorized', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'HostLogin' => array(self::HAS_ONE, 'HostLogin', array('id_host' => 'id_host')),
			'WhmAccount' => array(self::HAS_MANY, 'WhmAccount', array('id_host' => 'id_host')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_host' => 'Id Host',
			'name' => 'Name',
			'description' => 'Description',
			'uncategorized' => 'Uncategorized',
		);
	}
	
	public function __get($key)
	{
		$action = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
		if(method_exists($this, $action)){
			return $this->$action();
		}
		
		return parent::__get($key);
	}
	
	public function getNameDropdownLabel()
	{
		return 'Host: ' . $this->name;
	}
	
	public function getNameWithLoginLink()
	{
		if($this->HostLogin){
			return "
				<a href='".url('whm/host/login', array('id_host' => $this->id_host))."' target='_blank'>{$this->name}</a>
			";
		}else{
			return $this->name;
		}
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_host',$this->id_host);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
//		$criteria->compare('uncategorized',$this->uncategorized);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination' => array(
				'pageSize'=> app()->user->getData()->getIndividualConfig()->getPagination()
			)
		));
	}
	
	public function delete()
	{
		if($this->uncategorized == 1){
			$this->addError('Uncategorized Host', 'Uncategorized Host cannot be deleted');
			return false;
		}
		
		if(parent::delete()){
			// change host of host_login under this host to uncategorized host 
			app()->db->createCommand()->update(
				WhmAccount::model()->tableName(),
				array('id_host' => 0),
				'id_host = :pk',
				array(':pk' => $this->id_host)
			);
			
			return true;
		}
		
		return false;
	}
	
	public static function renderListActionButtons()
	{
		return '
			<div class="btn-group">
				<button class="btn btn-info dropdown-toggle actions" data-toggle="dropdown">
					'. t('buttons', 'Actions') .'
					<span class="caret"></span>
				</button>
				<ul class="dropdown-menu">
					<li class="host_accounts"><a href="javascript:void(0);"><i class="radmin-icon radmin-clipboard-2"></i> '. t('buttons', 'List Accounts at Host') .'</a></li>
					<li class="setup_hostlogin"><a href="javascript:void(0);"><i class="radmin-icon radmin-enter"></i> '. t('buttons', 'Setup Host Login') .'</a></li>
					<li class="host_edit"><a href="javascript:void(0);"><i class="radmin-icon radmin-pencil"></i> '. t('buttons', 'Edit Host') .'</a></li>
					<li class="host_delete"><a href="javascript:void(0);"><i class="radmin-icon radmin-remove"></i> '. t('buttons', 'Delete Host') .'</a></li>
				</ul>
				
				<div class="delete_confirm_btn hide">
					<a class="btn btn-small delete_confirm"><i class="icon-ok"></i>'. t('buttons', 'Confirm Delete') .'</a>
					<a class="btn btn-small delete_cancel"><i class="icon-remove"></i>'. t('buttons', 'Cancel') .'</a>
				</div>
			</div>
		';
	}
}